﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
namespace BUS
{
    public class OrderBUS
    {
        public static bool insertoOrder(ORDER oOrder)
        {
            try
            {
                oOrder.VISIBLE = true;
                oOrder.TIME_ORDER = DateTime.Now;
                oOrder.STATUS_ORDER = 0;
                PRODUCT Product = ProductBUS.GetProduct(oOrder.IDPRODUCT);
                if (oOrder.QUANTITY <= 0)
                    return false;
                if ((Product.OrderedNumber + oOrder.QUANTITY) > Product.MAX_NUMBER)
                    return false;
                oOrder.PAYMENT = Product.PRICE * oOrder.QUANTITY;
                //ProductBUS.AddOrderNumber(oOrder.IDPRODUCT,(int)oOrder.QUANTITY);
                return OrderDAO.insertOrder(oOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ORDER> GetOrderByMember(int IDMember)
        {
            try
            {
                if (MemberDAO.IsExisting(IDMember)||ManagerDAO.IsExisting(IDMember)||AdministratorDAO.IsExisting(IDMember))
                    return OrderDAO.GetOrderByMember(IDMember);
                return new List<ORDER>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ORDER> GetOrderByProduct(int IDProduct)
        {
            try
            {
                bool isExist = ProductDAO.IsExisting(IDProduct);
                if (!isExist)
                    return new List<ORDER>();
                return OrderDAO.GetOrderByProduct(IDProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Delete(int IDOrder)
        {
            try
            {
                bool isExist = OrderDAO.IsExisting(IDOrder);
                if (!isExist)
                    return false;
                return OrderDAO.Delete(IDOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static ORDER GetOrder(int idOrder)
        {
            try
            {
                bool isExist = OrderDAO.IsExisting(idOrder);
                if (!isExist)
                    return null;
                return OrderDAO.GetOrder(idOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Update(ORDER Order)
        {
            try
            {
                bool isExist = OrderDAO.IsExisting(Order.IDORDER);
                if (!isExist)
                    return false;
                return OrderDAO.Update(Order);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
